//
//  LKAudioController.m
//  音视频合成demo
//
//  Created by VictorLiu on 2018/10/19.
//  Copyright © 2018 VictorLiu. All rights reserved.
//

#import "LKAudioController.h"
#import "LKRecordAudioCell.h"

@interface LKAudioController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *audioArray;


@property (weak, nonatomic) IBOutlet UILabel *mergePathLabel;

@property (weak, nonatomic) IBOutlet UIButton *mergePlayButton;

@property (nonatomic, strong) NBRecordAudioModel *mixAudio;

@end

@implementation LKAudioController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpContent];
}

- (void)setUpContent {
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 500;
    
}

- (NSMutableArray *)audioArray {
    if (!_audioArray) {
        _audioArray = [NSMutableArray array];
    }
    return _audioArray;
}

#pragma mark --- 事件
- (IBAction)recorderButtonTouchDown:(UIButton *)sender {
    [sender setTitle:@"松开停止录音" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor lightGrayColor];
    __weak typeof(self) weakSelf = self;
    [NBAVTool startRecordAudio:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)recorderButtonTouchUpInside:(UIButton *)sender {
    [sender setTitle:@"按住说话" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    [NBAVTool stopRecordAudio:^(BOOL success, NSString *errorStr, NBRecordAudioModel *audio) {
        if (success) {
            [weakSelf.audioArray addObject:audio];
            [weakSelf.tableView beginUpdates];
            [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.audioArray.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            [weakSelf.tableView endUpdates];
            
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)recorderButtonTouchUpOutSide:(UIButton *)sender {
    
    [sender setTitle:@"按住说话" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    [NBAVTool stopRecordAudio:^(BOOL success, NSString *errorStr, NBRecordAudioModel *audio) {
        if (success) {
            [weakSelf.audioArray addObject:audio];
            [weakSelf.tableView beginUpdates];
            [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.audioArray.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            [weakSelf.tableView endUpdates];
            
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}

- (IBAction)mergeButtonClicked:(id)sender {
    NSMutableArray *pathArray = [NSMutableArray array];
    for (int i =0; i < self.audioArray.count; i++) {
        NBRecordAudioModel *audioModel = self.audioArray[i];
        [pathArray addObject:audioModel.path];
    }
    __weak typeof(self) weakSelf = self;
    [NBAVTool mergeAudio:pathArray completionHandle:^(BOOL success, NSString *errorStr, NBRecordAudioModel *audio) {
        if (success) {
            weakSelf.mixAudio = audio;
            weakSelf.mergePathLabel.hidden = NO;
            weakSelf.mergePlayButton.hidden  =NO;
            weakSelf.mergePathLabel.text = audio.path;
        } else {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


- (IBAction)mergePlayButtonClicked:(id)sender {
    __weak typeof(self) weakSelf = self;
    [NBAVTool playAudioPath:self.mixAudio.path withCompletionHandle:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


#pragma mark --- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.audioArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LKRecordAudioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKRecordAudioCell" forIndexPath:indexPath];
    cell.audioModel = self.audioArray[indexPath.row];
    cell.indexPath = indexPath;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) weakSelf = self;
    cell.deleteBlock = ^(NBRecordAudioModel *audio, NSIndexPath *indexPath) {
        [weakSelf.audioArray removeObject:audio];
        [weakSelf.tableView beginUpdates];
        [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [weakSelf.tableView endUpdates];
        [NBAVTool deleteLocalRecordAudio:audio.path finish:^(BOOL success, NSString *errorStr) {
            if (!success) {
                [weakSelf.view makeToast:errorStr];
            }
        }];
        [weakSelf.tableView reloadData];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NBRecordAudioModel *audioModel = self.audioArray[indexPath.row];
    __weak typeof(self) weakSelf = self;
    [NBAVTool playAudioPath:audioModel.path withCompletionHandle:^(BOOL success, NSString *errorStr) {
        if (!success) {
            [weakSelf.view makeToast:errorStr];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
